<%@ page language="java"
	import="java.util.*,com.forum.domain.*,com.forum.controller.*"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";

	TopicDAO tDao = new TopicDAO();
	int topicid = Integer.parseInt(request.getParameter("topicid"));
	Topic topic = tDao.findById(topicid);
	Integer id = (Integer) request.getSession().getAttribute("id");
	UserDAO userdao = new UserDAO();
	User user = userdao.findById(id);

	ReplyDAO rDao = new ReplyDAO();
	List<Reply> replylist = rDao.findByProperty("topic", topic);

	UctDAO uctDAO = new UctDAO();
	List<Uct> uctlist = uctDAO.findByProperty("topic", topic);
	boolean follow = false;
	for (int i = 0; i < uctlist.size(); i++) {
		if (uctlist.get(i).getUser() == user) {
			follow = true;
			break;
		}
	}

	System.out.println();

	Boolean fo = false;
	// 	if(true/*userlist.contains(user)*/)
	//	{
	//	fo=true;
	//	}
	//	else fo=false;

	/*	int topicid = 1;
	 int userid = 1;
	 TopicDAO td = new TopicDAO();
	 Topic topic = td.findById(topicid);
	 */
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>

		<base href="<%=basePath%>">

		<title>My JSP 'topic.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<link rel="stylesheet" type="text/css" href="css/topic.css">
		<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>

		<script type="text/javascript">
		<%if (follow == true)
				out.print("var is_follow = true;");
			else
				out.print("var is_follow = false;");
		out.print("var id="+id+";");
		out.print("var topicid="+topicid+";");		
		%>
				
			function clickFollow()
			{
			
			if(is_follow == true)
				{
				$.ajax({ type : "POST",
						url : "servlet/UnfollowTopic",
							data : "type=unfollow" + "&id=" + id +"&topicid="+topicid,
							success : function(msg) {
							is_follow = false;
							$("#followit span").text("Follow");
							}
						});
				}		
				else
				{		$.ajax({ type : "POST",
						url : "servlet/UnfollowTopic",
							data : "type=follow" + "&id=" + id +"&topicid="+topicid,
							success : function(msg) {
							is_follow = true;
							$("#followit span").text("Unfollow");
							}
						});
				}
			}
		
		
		function onClickReply(id){
			//document.getElementById('AtUser').value=document.getElementById('reply-item-').value
			var name = $("#reply-item-"+id).text();
			var replyuserid = $("#reply-item-"+id).attr("value");
			$("#AtUser").val(name);
		};
		
		
			function onClickSubmit(){
			
				var content = $("#Content").val();
				if( content =="")
				{
					return false;
				}
				else
					return true;
			};
			
			
	
</script>

	</head>

	<body>
		<div id="page">


			<div class="main-content">
				<div class="header_section">
					<div class="header_container">
					</div>
				</div>

				<div class="content section">
					<div class="container">
						<div class="clearfix">
							<div class="span-7 last">
								<h1 class="entry-title">
									<%=topic.getTitle()%>
								</h1>
								<div class="entry-meta">
									<div class="entry-author">
										<a><%=topic.getUser().getNickname()%> </a>
									</div>
									<div class="entry-time"><%=topic.getDateTime()%></div>

									<div class="replies-count">
										<a href="page/topic.jsp#replies"> <%=topic.getReplycount()%>Replies
										</a>
									</div>
								</div>

								<div id="sticky" style="margin-top: 0px; positon: abusolute;">
									<div class="followit">
										<a href="page/home.jsp" class="followit"> <span
											class="followit-text">返回主页</span> <span class="count"
											id="fanhui"></span> </a>
										<a class="followit" id="followit"
											href="javascript:function(void)" onclick="clickFollow()">
											<span class="followit-text"> <%
 	if (follow == true)
 		out.print("Unfollow");
 	else
 		out.print("Follow");
 %> </span> <span class="count"> <%=topic.getReplycount()%> </span> </a>
									</div>
								</div>
								<a name="topictext"></a>
								<div class="post">
									<div class="entry-category">

									</div>
									<div class="text">
										<div class="entry-content">

											<p></p>
											<%=topic.getContent()%>
											<p></p>

										</div>
									</div>


									<div class="span-8 last">

										<h4 id="replies">
											<%=topic.getReplycount()%>
											Responses to
											<a href="page/topic.jsp#topictext"><%=topic.getTitle()%></a>
										</h4>

										<a name="replies"></a>
										<ul class="replylist">
											<%
												for (int i = 0; i < replylist.size(); i++) {
													Reply reply = replylist.get(i);
											%>

											<li class="even thread-even depth-1" id="">
												<div class="reply-body clearfix">
													<div class="reply-user-pic">
														<img height="48" width="48"
															src="upload/<%=reply.getUserByUserFromId().getPicurl()%>"
															data-user-id="3">
													</div>
													<div class="reply-content span-6">
														<div class="reply-user-name">
															<a class="reply-user-nickname"
																value="<%=reply.getUserByUserFromId().getUserid()%>"
																id="reply-item-<%=i%>"><%=reply.getUserByUserFromId().getNickname()%></a>

															<span class="reply-user-realname"><%=reply.getUserByUserFromId().getRealname()%></span>

														</div>
														<p>

															<%=reply.getContent()%>
														</p>
													</div>
													<div class="span-1 reply-meta last">
														<a class="reply-date"> <span class="reply-day"><%=reply.getDay()%></span>
															<span class="reply-month"><%=reply.getMonth()%></span> </a>
														<span class="reply-time"><%=reply.getDateTime()%></span>
													</div>

													<a class="reply-link" onclick="onClickReply(<%=i%>)"
														href="page/topic.jsp?topicid=<%=topic.getId()%>#respond"">Reply</a>
												</div>
											</li>
											<%
												}
											%>
										</ul>



										<div id="reply-container">
											<a name="respond"></a>
											<h4 id="reply-post">
												Post your reply
											</h4>
											<form id="respondform" action="servlet/AddReply"
												method="POST" onsubmit="return onClickSubmit();">
												<div class="respond-meta">
													<p class="respond-field form-item">
														<label for="Tags">
															Tags
														</label>
														<input type="text" name="Tags" value size="22"
															tabindex="1" class="required">

													</p>
													<p class="respond-field form-item">
														@
														<input type="text" name="AtUser" id="AtUser" value=""
															ref="" size="22" tabindex="2" class="required">
														<label for="AtUser">
															Name
														</label>
														<!--<span class="form-required" title="This field is required">This field is required</span>
							-->
													</p>
												</div>
												<div class="form-item">
													<textarea name="respond" id="Content" tabindex="4"
														rows="10" cols="40" style="padding: 15px"></textarea>
												</div>
												<input type="hidden" value="<%=id%>" name="userid">
												<input type="hidden" value="<%=topic.getId()%>"
													name="topicid">
												<input class="respond-submit" name="submit" type="submit"
													id="reply-button" tabindex="5" value="Submit Reply">
											</form>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>




			</div>
		</div>

		<div class="sidebar">
		</div>

	</body>
</html>
